﻿//TODO: check filter, SkinFilter1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    //TODO: check IsSkin Formula
    /// <summary>
    /// Skinfilter
    /// </summary>
    public class SkinFilter1 :  BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter1"/> class.
        /// </summary>
        public SkinFilter1()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter1"/> class.
        /// </summary>
        /// <param name="_colorForeground">The _color foreground.</param>
        /// <param name="_colorBackground">The _color background.</param>
        public SkinFilter1(Color _colorForeground, Color _colorBackground)
        {
            m_ColorForeground = _colorForeground;
            m_ColorBackground = _colorBackground;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color colorToSet = Color.Transparent;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;
                
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    colorToSet = IsSkin(color) ? m_ColorForeground : m_ColorBackground;
                    bitmap.SetPixel(x, y, colorToSet);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Determines whether the specified color is skin.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns>
        /// 	<c>true</c> if the specified color is skin; otherwise, <c>false</c>.
        /// </returns>
        private bool IsSkin(Color color)
        {
            return ((((((((color.R > 0x5f) & (color.G > 40)) & (color.B < 20)) & ((ColorFunctions.MaxChannel(color) - ColorFunctions.MinChannel(color)) > 15)) & (Math.Abs((int)(color.R - color.G)) > 15)) & (color.R > color.G)) & (color.R > color.B)) || ((((((color.R > 220) & (color.G > 210)) & (color.B > 170)) & (Math.Abs((int)(color.R - color.G)) <= 15)) & (color.R > color.B)) & (color.G > color.B)));
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SkinFilter1();
        }
    }
}
